package com.yingxin.yx.framework.commons.helper;

import com.yingxin.yx.framework.commons.exception.BizException;

import java.util.List;
import java.util.function.Function;

public class UniqueHelper {

    public static <T> String check(String val, T t, Function<T, String> function, String msg) {
        if (val == null) {
            val = function.apply(t);
        } else if (!val.equals(function.apply(t))) {
            throw new BizException(msg);
        }
        return val;
    }

    public static <T> String check(String val1, String val2, String msg) {
        if (val1 == null) {
            val1 = val2;
        } else if (!val1.equals(val2)) {
            throw new BizException(msg);
        }
        return val1;
    }

    public static <T> void check(String val, List<T> list, Function<T, String> function, String msg) {
        for (T t : list) {
            if (!val.equals(function.apply(t))) {
                throw new BizException(msg);
            }
        }
    }

}
